﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Magic_Folder
{
    public partial class Main : Form
    {
        bool closing = false;
        public Main()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Program_Item item = new Program_Item();
            lst_Programs.Controls.Add(item);
            item.Dock = DockStyle.Top;
            item.Focus();
            item.BringToFront();
            item.KeyDown += lst_item_keydown;
        }


        private void lst_item_keydown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            int lstindex=0, count =0;
            if(e.KeyData == Keys.Down)
            {
                foreach(Control control in lst_Programs.Controls)
                {
                    count++;
                    if (control.ContainsFocus)
                    {
                        lstindex = count;
                    }
                }
                count = 0;
                foreach (Control control in lst_Programs.Controls)
                {
                    
                    if (count == (lstindex +1))
                    {
                        control.Focus();
                    }
                }


            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (Control control in lst_Programs.Controls)
            {
                if (control.ContainsFocus)
                {
                    control.BackColor = Color.SteelBlue;
                }
                else
                {
                    control.BackColor = Color.WhiteSmoke;
                }
            }
            foreach (lst_Rule control in lst_Rules.Controls)
            {
                if (control.ContainsFocus)
                {
                    control.BackColor = Color.SteelBlue;
                    control.tabControl1.Visible = true;
                    control.Height = 171;
                }
                else
                {
                    control.BackColor = Color.WhiteSmoke;
                    control.tabControl1.Visible = false;
                    control.Height = 51;
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            closing = true;
            Application.Exit();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = !closing;
            this.Hide();
        }

        private void showSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            lst_Rule item = new lst_Rule();
            lst_Rules.Controls.Add(item);
            item.Dock = DockStyle.Top;
            item.Focus();
            item.BringToFront();
            item.KeyDown += lst_item_keydown;
        }

        private void rule1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            foreach (Program_Item control in lst_Programs.Controls)
            {
                control.Visible = control.lbl_FileDescription.Text.ToLower().Contains(textBox1.Text.ToLower());
            }
        }

        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            MessageBox.Show(e.FullPath.ToString());
        } 
    }
}
